package com.iweb.order.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iweb.order.model.Order;
import com.iweb.order.service.OrderService;
import com.iweb.order.service.OrderServiceImpl;
import com.iweb.shopping.model.Address_sh;
import com.iweb.shopping.model.Goods;
import com.iweb.shopping.service.ShoppingService;
import com.iweb.shopping.service.ShoppingServiceImpl;
import com.iweb.user.model.User;
import com.iweb.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "*.order")
public class OrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        String path = req.getServletPath();
        switch (path){
            case"/toList.order":
                req.getRequestDispatcher("/shopping/order_selectByPage.html").forward(req,resp);
                break;
            case"/selectByPage.order":
                selectByPage(req,resp);
                break;
            case"/deleteById.order":
                deleteById(req,resp);
                break;
            case"/toBuy.order": // 购买
                toBuy(req,resp);
                break;
            case"/saveOne.order":
                saveOne(req,resp);
                break;
            default:
                break;
        }
    }

    OrderService orderService = new OrderServiceImpl();
    ShoppingService shoppingService = new ShoppingServiceImpl();

    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("user") == null){
            resp.getWriter().print("");
            return;
        }

        String pageNo = req.getParameter("pageNo") == null?"1":req.getParameter("pageNo");
        String likeValue = req.getParameter("likeValue") == null?"":req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(Integer.valueOf(pageNo));
        pageModel.setLikeValue(likeValue);

        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();

        List<Order> orders = orderService.selectByPage(pageModel,userid);
        System.out.println(orders.size());
        pageModel.setRecordList(orders);
        String jsonStr = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(jsonStr);

    }
    public void deleteById (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderid = req.getParameter("orderid");
        orderService.deleteOne(Integer.valueOf(orderid));
        resp.sendRedirect(req.getContextPath()+"/toList.order?pageNo=1&likeValue=");
    }

    public void toBuy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("goodsToken","goodsTokenValue");
        String goodsid = req.getParameter("goodsid");
        String cartid = req.getParameter("cartid");
        resp.sendRedirect(req.getContextPath()+"/shopping/order_submit.html?goodsid="+goodsid+"&cartid="+cartid);
    }
    public void saveOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        Object o = req.getSession().getAttribute("goodsToken");
        if (o!=null){
            req.getSession().removeAttribute("goodsToken");

            HttpSession session = req.getSession();
            Object userObj = session.getAttribute("user");
            User user = (User) userObj;
            Integer userid = user.getUserid();

            String goodsid = req.getParameter("goodsid");
            String cartid = req.getParameter("cartid");
            String address = req.getParameter("address");
            Goods goods = orderService.selectByGoodsid(Integer.valueOf(goodsid));
            Integer salerid = goods.getUserid();
            Address_sh address_sh = orderService.selectByaddress(address);
            Integer addressid = address_sh.getAddressid();
            Order order = new Order(null,Integer.valueOf(goodsid),userid,salerid,null,0,addressid);
            int i = orderService.saveOne(order);
            if (i>0){
            shoppingService.deleteOne(Integer.valueOf(cartid));
            resp.getWriter().print("success");
            }else {
            resp.getWriter().print("error");
            }
//            resp.sendRedirect(req.getContextPath()+"/shopping/order_success.html");
        }else {
            resp.sendRedirect(req.getContextPath()+"/shopping/token.html");
        }

    }

}
